/*
 *  Copyright (C) 2009 Dominik Psenner, Christoph Caks
 * 
 *   This file is part of project-blog.
 *
 *  project-blog is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  project-blog is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with project-blog.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.blog.cron;

import java.io.IOException;
import java.util.Collection;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blog.jdo.ArticleDAO;
import com.blog.jdo.PMF;
import com.blog.markup.MarkupTransformer;
import com.blog.markup.docbook.SaxonXSLTMarkupTransformer;
import com.blog.model.Article;

public class CompileArticlesServlet extends HttpServlet {
	private static final long serialVersionUID = 5256819219263852309L;
	private static final Logger log = Logger.getLogger("project-blog.servlets.CompileArticlesServlet");
	
	@Override
	protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
		log.entering(getClass().getName(), "doGet");
		final ArticleDAO articleDAO = new ArticleDAO();
		final Collection<Article> todo = articleDAO.getUncompiledArticles();
		
		for (final Article a : todo) {
			processArticle(a);
		}
		log.exiting(getClass().getName(), "doGet");
	}
	
	private void processArticle(final Article a) {
		log.entering(getClass().getName(), "processArticle", a);
		final PersistenceManager pm = PMF.get().getPersistenceManager();
		final Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			final Article article = pm.getObjectById(Article.class, a.getId());
			pm.makePersistent(compileArticle(article));
			tx.commit();
		}
		catch (final Exception e) {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		log.exiting(getClass().getName(), "processArticle");
	}
	
	private Article compileArticle(final Article a) {
		log.entering(getClass().getName(), "compileArticle", a);
		// MarkupTransformer docbook = new DocBookMarkupTransformer();
		final MarkupTransformer docbook = new SaxonXSLTMarkupTransformer();
		final String title = docbook.extractTitle(a.getSource());
		if (title != null) {
			a.setTitle(title);
			a.setContent(docbook.transform(a.getSource()));
			// TODO retrieve tags
			// retrieve title
			a.setCompiled(true);
		}
		else {
			// error with title
			log.warning("Could not extract title from document ==> revert");
		}
		log.exiting(getClass().getName(), "compileArticle", a);
		return a;
	}
}
